package logic

import (
	"context"
	"errors"
	"know-reg/models/cache"
	"know-reg/models/mysql"

	"know-reg/knowrpc/internal/svc"
	"know-reg/knowrpc/knowrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type UserPerfectLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUserPerfectLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserPerfectLogic {
	return &UserPerfectLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *UserPerfectLogic) UserPerfect(in *knowrpc.UserPerfectRequest) (*knowrpc.UserPerfectResponse, error) {
	// todo: add your logic here and delete this line

	var user mysql.User

	if err := user.FindUserById(mysql.DB, in.UserId); err != nil {
		return nil, errors.New("用户查询失败")
	}

	result1, _ := cache.Rdb.Get(l.ctx, "sendSms"+user.Mobile).Result()

	if result1 == "" {
		return nil, errors.New("短信验证码错误，请重新输入")
	}

	user.Age = in.Age
	user.Email = in.Email
	user.Address = in.Address

	if err := user.UpdateUser(mysql.DB); err != nil {
		return nil, err
	}

	return &knowrpc.UserPerfectResponse{
		UserId: int64(user.ID),
	}, nil
}
